package com.jinyi.wwdeng.ui.config.fragment

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.jinyi.base.base.BaseFragment
import com.jinyi.base.utils.HHLog
import com.jinyi.pickerview.builder.TimePickerBuilder
import com.jinyi.pickerview.listener.CustomListener
import com.jinyi.pickerview.listener.OnTimeSelectListener
import com.jinyi.pickerview.view.TimePickerView
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentPersonalInfoBinding
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/18 11:29
 * @Annotation：个人信息
 */
class PersonalInfoFragment: BaseFragment() {

    lateinit var binding: FragmentPersonalInfoBinding

    //性别 1男  2女
    private var mGenderType = 1
    //出生日期选择
    var pvStartDateView: TimePickerView? = null

    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentPersonalInfoBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        //男
        binding.btnMale.setOnClickListener {
            mGenderType = 1
            checkGender()
        }

        //女
        binding.btnFemale.setOnClickListener {
            mGenderType = 2
            checkGender()
        }

        //选择出生年月
        binding.tvBirth.setOnClickListener {
            showBirthDialog()
        }
    }

    override fun initData() {

    }

    /**
     * 切换性别
     */
    private fun checkGender(){
        binding.btnMale.setBackgroundResource(R.drawable.base_circle_fill_f8f8f8)
        binding.btnFemale.setBackgroundResource(R.drawable.base_circle_fill_f8f8f8)
        when(mGenderType){
            1 -> {
                binding.btnMale.setBackgroundResource(R.drawable.base_circle_fill_9fe76f)
            }
            else -> {
                binding.btnFemale.setBackgroundResource(R.drawable.base_circle_fill_9fe76f)
            }
        }
    }

    fun getTime(date: Date, key: String): String { //可根据需要自行截取数据显示
        val format = SimpleDateFormat(key)
        return format.format(date)
    }

    /**
     * 选择出生日期
     */
    private fun showBirthDialog(){
        if(pvStartDateView == null){
            //当前日期
            var curtDate = Calendar.getInstance()
            var startDate = Calendar.getInstance()
            var endDate = Calendar.getInstance()
            startDate.set(1900,0,1)

            pvStartDateView = TimePickerBuilder(context, OnTimeSelectListener { date, v ->
                HHLog.e(">>>>>>>>>>>>>>>>$date")
                binding.tvBirth.text = getTime(date,"yyyy-MM-dd")
            })
                .setDate(curtDate) //默认选中
                .setRangDate(startDate, Calendar.getInstance()) //可选择时间范围
                .setLayoutRes(R.layout.dialog_birth, object : CustomListener {
                    override fun customLayout(v: View?) {
                        val ivClose = v!!.findViewById<ImageView>(R.id.ivClose)
                        ivClose.setOnClickListener {
                            pvStartDateView!!.dismiss()
                        }
                        //确定
                        val btnDone = v!!.findViewById<TextView>(R.id.btnDone)
                        btnDone.setOnClickListener {
                            pvStartDateView!!.returnData()
                            pvStartDateView!!.dismiss()
                        }
                    }
                })
                .isDialog(true)
                .setContentTextSize(16)
                .setType(booleanArrayOf(true, true, true, false, false, false))
                .setLabel("", "", "", "", "", "")
                .setLineSpacingMultiplier(3f)
                .setTextXOffset(40, 40, 40, 40, 40, 40)
                .isCenterLabel(false)//是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setTextColorCenter(ContextCompat.getColor(requireContext(), R.color.color_08252c)) //设置选中项文字颜色
                .setTextColorOut(ContextCompat.getColor(requireContext(),R.color.color_9ca8ab))//默认颜色
                .setContentTextSize(16)
                .isAlphaGradient(true)
                .setItemVisibleCount(3)
                .isCyclic(true)
                .setDividerColor(ContextCompat.getColor(requireContext(), R.color.transparent))
                .build()
//        this.pvStartDateView!!.show()

            val dialog = pvStartDateView!!.dialog
            if (dialog != null){
                val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)
                params.leftMargin = 0
                params.rightMargin = 0
                pvStartDateView!!.dialogContainerLayout.setLayoutParams(params)
                val dialogWindow = dialog.window
                if (dialogWindow != null) {
                    dialogWindow.setWindowAnimations(com.jinyi.pickerview.R.style.picker_view_scale_anim) //修改动画样式
                    dialogWindow.setGravity(Gravity.CENTER)
                    dialogWindow.setDimAmount(0.3f)
                }
            }
        }
        pvStartDateView!!.show()
    }

}